package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.dao.entity.SingleChatDB;
import com.molyfun.parents.dao.gen.DaoMaster;
import com.molyfun.parents.dao.gen.DaoSession;
import com.molyfun.parents.dao.gen.SingleChatDBDao;
import com.molyfun.parents.rest.bean.TeamListResponse;
import com.molyfun.parents.utils.Const;
import com.tencent.imsdk.TIMConversation;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 班级下面小组
 */
public class NewTeamView extends LinearLayout implements View.OnClickListener, View.OnTouchListener {

    protected LayoutInflater layoutInflater;

    @InjectView(R.id.product_name)
    TextView mProductName;
    @InjectView(R.id.zhibo_state)
    ImageView mZhiboState;
    @InjectView(R.id.line)
    View mLine;
    @InjectView(R.id.msg_count)
    TextView mMsgCount;
    @InjectView(R.id.new_team_container_layout)
    RelativeLayout mTeamContainerLayout;
    private Context context;
    private TeamListResponse.TeamBean mTeamBean;
    protected TIMConversation conversation;

    public NewTeamView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
        init(context, attrs);
    }

    public NewTeamView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public NewTeamView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_new_team_cell, this);
        ButterKnife.inject(this);
        mProductName.setOnClickListener(this);
        mTeamContainerLayout.setOnClickListener(this);
    }

    public void initData(TeamListResponse.TeamBean teamBean) {
        initData(teamBean, false);
    }

    public void initData(TeamListResponse.TeamBean teamBean, boolean isLast) {
        if (teamBean == null) {
            return;
        }
        mTeamBean = teamBean;
        mProductName.setText(teamBean.getName());
        mTeamContainerLayout.setOnClickListener(this);

        if (!TextUtils.isEmpty(teamBean.getLivestatus()) && teamBean.getLivestatus().equals("prog")) {
            mZhiboState.setVisibility(View.VISIBLE);
        } else {
            mZhiboState.setVisibility(View.GONE);
        }

        if (isLast) {
            mLine.setVisibility(View.GONE);
        } else {
            mLine.setVisibility(View.VISIBLE);
        }

        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MFPApplication.getInstance(), Const.DB_NAME, null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        SingleChatDBDao mSingleChatDBDao = daoSession.getSingleChatDBDao();
        SingleChatDB singleChatDB = mSingleChatDBDao.queryBuilder().where(SingleChatDBDao.Properties.Conversationid.eq(mTeamBean.getImgroupid())).build().unique();
        int msgCount = 0;
        if(singleChatDB != null){
            msgCount = singleChatDB.getUnReadMsgCount();
        }

        if (msgCount > 0) {
            mMsgCount.setVisibility(View.VISIBLE);
            if (msgCount >= 100) {
                mMsgCount.setText("99+");
            } else {
                mMsgCount.setText(msgCount + "");
            }

        } else {
            mMsgCount.setVisibility(View.GONE);
        }

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN://0
                return false;
            case MotionEvent.ACTION_UP:
                mMsgCount.setVisibility(View.GONE);
                ClassRoomActivity.startActivity(getContext(), mTeamBean.getId(), mTeamBean.getImgroupid(), mTeamBean.getMembercount(),mTeamBean.getName(), mTeamBean.getLivestatus(), mTeamBean.getRule(), mTeamBean.isExpire(), true);
                return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                mMsgCount.setVisibility(View.GONE);
                ClassRoomActivity.startActivity(getContext(), mTeamBean.getId(), mTeamBean.getImgroupid(), mTeamBean.getMembercount(),mTeamBean.getName(), mTeamBean.getLivestatus(), mTeamBean.getRule(), mTeamBean.isExpire(), true);
                break;
        }
    }
}